<template>
<div class="bk">

<div id="login">
  <div id="main-box">
  <h1>律师管理系统登录界面</h1>
  <div class="login">
  <el-form  :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm" style="margin: 0px;padding: 0px">
    <el-form-item label="账号" prop="username">
      <el-input v-model="ruleForm.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" show-password></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
    <div style="display: flex;justify-content: flex-end">
      <el-link type="primary" style="margin-right: 10px" @click="toregister">注册</el-link>
    </div>
  </div>
</div>
  </div>
</template>

<script>
import axios from "axios";
import {login} from "@/api/vcard";

export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    toregister(){
      this.$router.push('register')
    },
    submitForm(formName) {
      console.log(formName)
      const _this=this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(_this.ruleForm).then(resp => {
            if(!resp.success){
              this.$notify.error({
                title: '错误',
                message: resp.message
              });
            }else {
              console.log(resp),
                  sessionStorage.setItem("user",JSON.stringify(resp.result))
                  this.$router.push({
                    name: 'Index'
                  })

            }

          })
        }else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.bk{
  background: url("../images/bk.png");
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  position: fixed;

}
#login{
  width: 30%;
  text-align: center;
  margin: 0 auto;
  margin-top: 10%;
  background: #ffffff99;
  padding: 13px;

}
#main-box{
  background: #ffffff;
  padding :56px 0 5px;
}

.login{
 margin: 50px auto 20px;
  width: 350px;
  height: 250px;
}
#login h1 {
  color: black;
}
</style>